<?php
// +----------------------------------------------------------------------
// | 分类管理－针对后台的商标、版权、专利等分类的添加、修改删除
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// |2015-11-20 下午3:48:35
// +----------------------------------------------------------------------
defined('BASEPATH') OR exit('No direct script access allowed');

class Catgory extends MY_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->helper('common');//加载公共函数
	}
	
	/**
	 * 分类管理首页
	 */
	public function index(){
		$where = array();
		$this->load->model('Catgory_model','catgory');//载入Menu模型类
		$count = $this->catgory->getCount();
		if ( $count > 0 ){
			$menus = $this->catgory->getData(1,$where,'title,id,pid,hide');
			$this->load->library('Tree');//载入第三方树形结构类
			$catgory = $this->tree->toFormatTree($menus);
		}	
		$this->load->vars('meta_title', '分类管理');
		$this->load->vars('catgory',$catgory);//分类显示
		$this->load->vars('record_count',$count);//数据总数
		$this->load->view('catgory/index');
	}
	
	/**
	 * 添加分类
	 */
	public function add(){
		$pid = intval( $this->uri->segment(3) );
		//引入表单验证类
		$this->load->library('form_validation');
		//验证表单及是post提交的时候才可以入库
		if( $this->form_validation->run('catgory') && IS_POST){
			$this->load->model('Catgory_model','catgory');//载入Catgory模型
			$insert_id = $this->catgory->addData();
			if ( $insert_id ){
				$this->show_message('success','添加成功');
			}else{
				$this->show_message('error','添加失败');
			}
		}else{
			$this->load->vars('meta_title', '添加分类');
			$catgory = $this->getCatgorytoTree();
			$this->load->vars('pid', $pid);
			$this->load->vars('catgory', $catgory);
			$this->load->view('catgory/add');
		}	
	}
	
	/**
	 * 修改分类信息
	 */
	public function modfiy(){
		$id = intval( $this->uri->segment(3,0) ) == 0 ? $this->input->get_post('id') : intval( $this->uri->segment(3) );
		if ( $id <= 0 ){
			$this->show_message(0, '参数错误', site_url('catgory/index'));
		}
		//引入表单验证类
		$this->load->library('form_validation');
		//验证表单及是post提交的时候才可以入库
		if( $this->form_validation->run('catgory') && IS_POST){
		$this->load->model('Catgory_model', 'catgory');
			$update_status = $this->catgory->updateDataByPostData(array('id'=>$id));
			if( $update_status ){
				$this->show_message('success','操作成功', site_url('catgory/index'));
			}else{
				$this->show_message('error','操作失败', site_url('catgory/modfiy/'.$id));
			}
		}else{
			//获取菜单详情
			$this->load->model('Catgory_model', 'catgory');//载入Menu模型
			$catgory_info = $this->catgory->getFind(1, array('id'=>$id),"*");
			$catgory = $this->getCatgorytoTree();//获取菜单树形结构
			$this->load->vars('meta_title', '修改分类');
			$this->load->vars('catgory_info',$catgory_info);
			$this->load->vars('catgory',$catgory);
			$this->load->view('catgory/add');
		}	
	}
	
	/**
	 * 删除分类信息
	 * @author Baip
	 */
	public function del(){
		$id = intval( $this->input->get_post('id',TRUE) );
		if ( 0 == $id || !is_numeric( $id ) ){
			ajaxreturn(0,'参数错误', site_url('catgory/index'));
		}
		//TODO检测是此分类下是否有数据，如果有数据不能删除
		
		$this->load->model('Catgory_model','catgory');//载入catgory模型类
		$state = $this->catgory->deleteData('id', $id);
		if ( $state ) {
			ajaxreturn(1,'操作成功', site_url('catgory/index'));
		}else{
			ajaxreturn(0,'参数错误', site_url('catgory/index'));
		}
	}
	
	/**
	 * 修改分类状态
	 */
	public function changstate(){
		$state = intval( $this->input->get('state',true) );
		$id    = intval( $this->input->get('id',true) );
		if ( 0 == $id || !is_numeric( $id ) ) {
			ajaxreturn(0,'参数错误', site_url('catgory/index'));
		}
		$this->load->model('Catgory_model','catgory');//载入catgory模型类
		$data['hide'] = $state;
		$state = $this->catgory->updateData($id,1,'id', $data);
		if ( $state ){
			ajaxreturn(1,'操作成功', site_url('catgory/index'));
		}else{
			ajaxreturn(1,'操作失败', site_url('catgory/index'));
		}
		
	}
}